/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.preRegistration.service;

import com.fzhucc.modules.preRegistration.domain.PreRegistration;
import com.fzhucc.modules.system.service.DictDetailService;
import com.fzhucc.modules.system.service.DictService;
import com.fzhucc.modules.system.service.dto.DictDetailDto;
import com.fzhucc.modules.system.service.dto.DictDto;
import com.fzhucc.modules.system.service.dto.DictQueryCriteria;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.preRegistration.repository.PreRegistrationRepository;
import com.fzhucc.modules.preRegistration.domain.dto.PreRegistrationDto;
import com.fzhucc.modules.preRegistration.domain.dto.PreRegistrationQueryCriteria;
import com.fzhucc.modules.preRegistration.service.mapstruct.PreRegistrationMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2023-01-09
**/
@Service
@RequiredArgsConstructor
public class PreRegistrationService {

    private final PreRegistrationRepository preRegistrationRepository;
    private final PreRegistrationMapper preRegistrationMapper;

    private final DictService dictService;

    private final DictDetailService dictDetailService;

    public Map<String,Object> queryAll(PreRegistrationQueryCriteria criteria, Pageable pageable){
        Page<PreRegistration> page = preRegistrationRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(preRegistrationMapper::toDto));
    }

    public List<PreRegistrationDto> queryAll(PreRegistrationQueryCriteria criteria){
        return preRegistrationMapper.toDto(preRegistrationRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    public PreRegistrationDto findById(Integer id) {
        PreRegistration preRegistration = preRegistrationRepository.findById(id).orElseGet(PreRegistration::new);
        ValidationUtil.isNull(preRegistration.getId(),"PreRegistration","id",id);
        return preRegistrationMapper.toDto(preRegistration);
    }

    @Transactional(rollbackFor = Exception.class)
    public PreRegistrationDto create(PreRegistration resources) {
        return preRegistrationMapper.toDto(preRegistrationRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(PreRegistration resources) {
        PreRegistration preRegistration = preRegistrationRepository.findById(resources.getId()).orElseGet(PreRegistration::new);
        ValidationUtil.isNull( preRegistration.getId(),"PreRegistration","id",resources.getId());
        preRegistration.copy(resources);
        preRegistrationRepository.save(preRegistration);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            preRegistrationRepository.deleteById(id);
        }
    }

    public void download(List<PreRegistrationDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PreRegistrationDto preRegistration : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", preRegistration.getName());
            map.put("项目名称", preRegistration.getProjectName());
            map.put("联系方式", preRegistration.getPhone());
            map.put("参赛组别   字典competition_category_type", preRegistration.getCompetitionCategory());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    public List<DictDetailDto> getCompetitionCategoryType() {
        DictQueryCriteria dictQueryCriteria = new DictQueryCriteria();
        dictQueryCriteria.setBlurry("competition_category_type");
        List<DictDto> list = dictService.queryAll(dictQueryCriteria);
        if (list != null && list.size() == 1) {
            List<DictDetailDto> list1 = dictDetailService.getDictByDictId(list.get(0).getId());
            return list1;
        }
        return null;
    }
}